# -*- coding: utf-8 -*-

from Validation_Contraintes import *
Validateur =  ValidationContrainte()
import xmlrpclib
#import pickle
import tkMessageBox as msgBox
try:
    from xTkinter import *
except:
    print >> sys.stderr, 'Le module xTkinter est  introuvable'  
    sys.exit(1)
import sys
Validateur = ValidationContrainte()
class Vue_EditionReglesAffaires(Frame):
    def __init__(self,Contrainte, master,parent,**cnf):
        KeysList=cnf.keys()
        self.parent = parent
        self.master=master
        self.width=400
        self.height=200
        if ('width' in KeysList):
            self.width=cnf['width']
        if ('height' in KeysList):
            self.height=cnf['height']
        Frame.__init__(self,master,cnf)        
        self.config(bg='Floralwhite')#(bg='#6B979C',relief='raised',bd=3)
        self.pack(side=LEFT, anchor=NW)
        self.configure(width=850,height=500)
        self.Contrainte = Contrainte
       
        
        # CREATION DU GUI RÈGLES D'AFFAIRES   
        
        
#        Width = 0
#        Position = 0
#        self.Attribut1_Label=xLabel(self,text='Attribut',height=1,width=20)
#        self.Attribut1_Label.setLocation(Width,(Position+1)*25+5)
#        Width = Width + 150
#        
#        self.Attribut_Operande_Gauche=xCombobox(self,width=20)
#        self.Attribut_Operande_Gauche.setLocation(Width,(Position+2)*25+5)
#        Width = Width + 140
#        
#        self.Comparaison_Label=xLabel(self,text='Comparaison',height=1,width=10)
#        self.Comparaison_Label.setLocation(Width,(Position+1)*25+5)
#        Width = Width + 70
#        
#        self.Comparaison_Combo_Box=xCombobox(self)
#        self.Comparaison_Combo_Box.width=10
#        self.Comparaison_Combo_Box.setListWidth(25)
#        self.Comparaison_Combo_Box.setListHeight(5)
#        self.Comparaison_Combo_Box.setLocation(Width,(Position+1)*25+5)
#        Width = Width + 15
        
#        self.Table_Operande_Droite=xCombobox(self,width=20)
#        self.Table_Operande_Droite.setLocation(Width,(Position+1)*25+5)
#        
#        self.Table2_Label=xLabel(self,text='Opérande',height=1,width=20)
#        self.Table2_Label.place(x=550,y=150)
#        
#        self.Attribut2_Label=xLabel(self,text='Attribut',height=1,width=20)
#        self.Attribut2_Label.place(x=695,y=150)
#        
#        self.Attribut_Operande_Droite=xCombobox(self,width=20)
#        self.Attribut_Operande_Droite.place(x=695,y=175)
#        
#        self.Message_Erreur_Text=xEntry(self,text='Message d''erreur',width=60)
#        self.Message_Erreur_Text.place(x=255,y=365)
#        
#        self.Message_Erreur_Label=xLabel(self,text='Message d''erreur',height=1,width=15)
#        self.Message_Erreur_Label.place(x=125,y=365)
#                
#        self.Conseils_Label=xLabel(self,text='Conseils',height=1,width=10)
#        self.Conseils_Label.place(x=185,y=405)
#        
#        self.Conseils_Text=xEntry(self,text='Conseils',width=60)
#        self.Conseils_Text.place(x=255,y=405)
#        
        xButton(self,text="Fermer",width=12, command = self.Quitter ).setLocation(16,0)

#        self.Parent_Combo_Box=xCombobox(self,width=8)
#        self.Parent_Combo_Box.place(x=10,y=175)
#        self.Parent_Label=xLabel(self,text='Parent',height=1,width=8)
#        self.Parent_Label.place(x=10,y=150)
       
        self.Condition_Combo_Box=xCombobox(self,width=8)
        self.Condition_Combo_Box.place(x=90,y=175)
        self.Condition_Label=xLabel(self,text='Condition',height=1,width=8)
        self.Condition_Label.place(x=90,y=150)
                
#        self.Table_Operande_Gauche=xCombobox(self,width=20)
#        self.Table_Operande_Gauche.place(x=165,y=175)
#        self.Table1_Label=xLabel(self,text='Opérande',height=1,width=20)
#        self.Table1_Label.place(x=165,y=150)
        
        self.Attribut1_Label=xLabel(self,text='Attribut',height=1,width=20)
        self.Attribut1_Label.place(x=310,y=150)
        self.Attribut_Operande_Gauche=xCombobox(self,width=20)
        self.Attribut_Operande_Gauche.place(x=310,y=175)
        
        self.Comparaison_Label=xLabel(self,text='Comparaison',height=1,width=10)
        self.Comparaison_Label.place(x=460,y=150)
        self.Comparaison_Combo_Box=xCombobox(self)
        self.Comparaison_Combo_Box.width=10
        self.Comparaison_Combo_Box.setListWidth(25)
        self.Comparaison_Combo_Box.setListHeight(5)
        self.Comparaison_Combo_Box.place(x=460,y=175)
        
        self.Table_Operande_Droite=xCombobox(self,width=20)
        self.Table_Operande_Droite.place(x=550,y=175)
        self.Table2_Label=xLabel(self,text='Opérande',height=1,width=20)
        self.Table2_Label.place(x=550,y=150)
        
        self.Attribut2_Label=xLabel(self,text='Attribut',height=1,width=20)
        self.Attribut2_Label.place(x=695,y=150)
        self.Attribut_Operande_Droite=xCombobox(self,width=20)
        self.Attribut_Operande_Droite.place(x=695,y=175)
        
        self.Message_Erreur_Text=xEntry(self,text='Message d''erreur',width=60)
        self.Message_Erreur_Text.place(x=255,y=365)
        self.Message_Erreur_Label=xLabel(self,text='Message d''erreur',height=1,width=15)
        self.Message_Erreur_Label.place(x=125,y=365)
                
        self.Conseils_Label=xLabel(self,text='Conseils',height=1,width=10)
        self.Conseils_Label.place(x=185,y=405)
        self.Conseils_Text=xEntry(self,text='Conseils',width=60)
        self.Conseils_Text.place(x=255,y=405)
        
        self.AssociationColonneChamps = {
                                        "ATTRIBUT_GAUCHE":self.Attribut_Operande_Gauche,
                                        "CONDITION":self.Condition_Combo_Box,
                                        "COMPARAISON":self.Comparaison_Combo_Box,
                                        "OPERANDE_DROITE":self.Table_Operande_Droite,
                                        "ATTRIBUT_DROITE":self.Attribut_Operande_Droite,
                                        "MESSAGE_ERREUR":self.Message_Erreur_Text,
                                        "MESSAGE_GUIDE":self.Conseils_Text,
                                        "ATTRIBUT_GAUCHE":self.Attribut_Operande_Gauche,
                                        "ID_CONTRAINTES":None,
                                        "OPERANDE_GAUCHE":None}
        self.initialiserComboBox()
        self.RemplirChamp(self.Contrainte)
        pass

    def Quitter(self):
        #if MsgBox.askokcancel("Attention", "Vous annulez l'ouverture de session."):
        self.destroy()
        #self.master.destroy()
        Contrainte = {}
        for champs in self.AssociationColonneChamps.keys():
            if self.AssociationColonneChamps[champs] <> None:
                Contrainte[champs] = self.AssociationColonneChamps[champs].getText()
        if self.Contrainte <> None:
            Contrainte['ID_CONTRAINTES'] = self.Contrainte['ID_CONTRAINTES']
        else:
            Contrainte['ID_CONTRAINTES'] = ''
        self.parent.MettreAJourContrainte(Contrainte)
        pass
    
    def close(self):
        self.destroy()
        pass
    
    def initialiserComboBox(self):
        #operande
        for Operande in Validateur.getOperande():
            self.Comparaison_Combo_Box.addItem(Operande)
                
    def RemplirChamp(self, Contrainte):
        print Contrainte
        if self.Contrainte == None:
            for Champs in self.AssociationColonneChamps.keys():
                if self.AssociationColonneChamps[Champs] <> None:
                    self.AssociationColonneChamps[Champs].setText('')
        else:
            for Champs in self.Contrainte:
                if self.AssociationColonneChamps[Champs] <> None:
                    self.AssociationColonneChamps[Champs].setText(Contrainte[Champs])
        pass
        
class Controleur:
    def __init__(self):
        Serveur='http://localhost:8000'
        self.serveur=xmlrpclib.ServerProxy(Serveur)
        root=Tk()
        root.geometry('870x520')
        self.Contrainte = {"OPERANDE_GAUCHE":'S_EMP', 
                                      "CONDITION":'IF', 
                                      "COMPARAISON":'>',
                                      "OPERANDE_DROITE":'18',
                                      "ATTRIBUT_DROITE":None, 
                                      "OPERANDE_GAUCHE":None, 
                                      "ATTRIBUT_GAUCHE":'AGE',
                                      "MESSAGE_ERREUR":'L''age minimum requis est 18 ans.', 
                                      "MESSAGE_GUIDE":'La personne doit etre majeure, i.e. avoir plus de 18 ans.', 
                                      "ID_CONTRAINTES":'1'
                                      }
        vi=Vue_EditionReglesAffaires(None, root,self)
        root.mainloop()       
        
#Controleur = Controleur()
     
